<?php
namespace OrderAdmin\Controller;
use Common\Service\AdminAuth;
use OrderAdmin\Model\AdminsModel;
use OrderAdmin\Model\OrderModel;
use OrderAdmin\Model\WorkerModel;
use Think\Auth;
use Think\PageAdmin;

/**
 * 师傅管理控制器
 */
class AdminController extends BaseController
{

    protected $adminModel = null;

    /**
     * 初始化
     *
     * @return [type] [description]
     */
    public function _initialize()
    {
        parent::_initialize();
        $this->adminModel = new AdminsModel();
    }

    public function index(){
        $this->display();
    }

    /**
     * 获取列表
     */
    public function getData(){
        $param['key'] = I('post.key', '');
        $param['status'] = I('post.status', '');
        $param['page'] = I('post.page', 1, 'intval');
        $param['size'] = I('post.size', 10, 'intval');
        $data = $this->adminModel->getData($param);
        ajax_return($data['status'], '', $data['data']);
    }

    /**
     * 获取当前用户信息
     */
    public function getUserInfo(){
        $id = AdminAuth::logined_id();
        $data = M('admins')->where(['id'=>$id])->find();
        ajax_return(true, '', $data);
    }

    public function user(){
        $this->display();
    }

    /**
     * 修改当前用户信息
     */
    public function edit(){
        $id = I('post.id', '');
        if(!$id){
            ajax_return(false, '用户不存在');
        }
        $param['avatar'] = I('post.avatar', '');
        $param['nickname'] = I('post.nickname', '');
        $param['password'] = I('post.password', '');
        foreach ($param  as $k=>$v){
            if($v === ''){
                unset($param[$k]);
            }
        }
        if($param['password']){
            $param['password'] = hash_make($param['password']);
        }
        $res = M('admins')->where(['id'=>$id])->save($param);
        $res ? ajax_return(true, '修改成功') : ajax_return(false, '修改失败');
    }

    /**
     * 编辑管理员
     */
    public function editother(){
        $id = I('get.id', 0, 'intval');
        if($id === 0){
            $this->error("请选择用户");
        }
        $this->assign(compact('id'));
        $this->display();
    }

    /**
     * 获取管理员信息
     */
    public function getUserById(){
        $id = I('post.id', 0, 'intval');
        $data = M('admins')->where(['id'=>$id])->find();
        if($data){
            ajax_return(true, '', $data);
        }else{
            ajax_return(false, '获取用户信息失败');
        }
    }

    /**
     * 修改管理员
     */
    public function editById(){
        $id = I('post.id', 0, 'intval');
        if($id === 0){
            ajax_return(false, '用户不存在');
        }
        $param['status'] = I('post.status', '');
        $param['nickname'] = I('post.nickname', '');
        $param['username'] = I('post.username', '');
        $param['password'] = I('post.password', '');
        if($param['password']){
            $param['password'] = hash_make($param['password']);
        }

        $param['updated_at'] = time();
        $res = M('admins')->where(['id'=>$id])->save($param);
        if($res !== false){
            ajax_return(true, '修改成功');
        }else{
            ajax_return(false, '修改失败');
        }
    }

    /**
     * 添加管理员
     */
    public function add(){
        $param['status'] = I('post.status', '');
        $param['nickname'] = I('post.nickname', '');
        $param['username'] = I('post.username', '');
        $param['password'] = I('post.password', '');
        if($param['password']){
            $param['password'] = hash_make($param['password']);
        }

        $param['created_at'] = time();
        $param['updated_at'] = time();
        $res = M('admins')->add($param);
        if($res !== false){
            ajax_return(true, '添加成功');
        }else{
            ajax_return(false, '添加失败');
        }
    }

    /**
     * 删除管理员
     */
    public function delete(){
        $id = I('post.id', 0, 'intval');
        if($id === 0){
            ajax_return(false, '用户不存在');
        }
        $res = M('admins')->where(['id'=>$id])->delete();
        $res ? ajax_return(true, '删除成功') : ajax_return(false, '删除失败');
    }

    public function addemployee(){
        $this->display();
    }

    /**
     * 获取所有的项目管理员下拉框信息
     */
    public function getManagerIds(){
        $data = M('admins')->field('nickname, id')->where(['status'=>'3'])->order('nickname asc')->select();
        ajax_return(true, '', $data);
    }
}